package com.example.app2024.viewmodel;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.app2024.R;
import com.example.app2024.databinding.ActivityViewModelBinding;

import java.sql.DatabaseMetaData;

public class ViewModelActivity extends AppCompatActivity
{

      MyViewModel myViewModel;
      Button btn;
      TextView textView;
      ImageButton imageButton;
      MyViewModelWithLiveData viewModelWithLiveData;
      Button button4;
      ActivityViewModelBinding binding;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {

            super.onCreate(savedInstanceState);
            EdgeToEdge.enable(this);
            // setContentView(R.layout.activity_view_model);
             binding = DataBindingUtil.setContentView(this, R.layout.activity_view_model);
            myViewModel = new ViewModelProvider(this).get(MyViewModel.class);

            btn = findViewById(R.id.button3);
            textView = findViewById(R.id.textView5);
            textView.setText(String.valueOf(myViewModel.number));
            binding.btn66.setText("binding.btn66.setText重新绑定");


            btn.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        myViewModel.number++;
                        textView.setText(String.valueOf(myViewModel.number));
                  }
            });

            imageButton = findViewById(R.id.imageButton);
            viewModelWithLiveData = new ViewModelProvider(this).get(MyViewModelWithLiveData.class);
            viewModelWithLiveData.getLikedNumber().observe(
                    this, new Observer<Integer>()
                    {
                          @Override
                          public void onChanged(Integer integer)
                          {
                                textView.setText(String.valueOf(integer));
                          }
                    });

            imageButton.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        viewModelWithLiveData.addLikedNumber(1);
                  }
            });

            button4 = findViewById(R.id.button4);
            button4.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        viewModelWithLiveData.addone();
                  }
            });

      }
}